﻿using BaseUI;
using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Web.UI.WebControls;
using Utility.Helper;

namespace Thksoft.Module.ProjectDetails
{
    public partial class BankList_ProjectDetails : BasePage
    {
        public int GetYear
        {
            get
            {
                return Convert.ToInt32(FORM_KEY1.Value);
            }
        }

        public int? GetMonth
        {
            get
            {
                if (FORM_KEY2.Value.Trim() == "")
                {
                    return null;
                }
                return Convert.ToInt32(FORM_KEY2.Value);
            }
        }

        public int? GetWeek
        {
            get
            {
                if (FORM_KEY3.Value.Trim() == "")
                {
                    return null;
                }
                return Convert.ToInt32(FORM_KEY3.Value);
            }
        }

        public int GetProId
        {
            get
            {
                return Convert.ToInt32(FORM_KEY4.Value);
            }
        }

        public int GetCompanyId
        {
            get
            {
                return Convert.ToInt32(FORM_KEY5.Value);
            }
        }

        public int? NId
        {
            get
            {
                if (FORM_KEY6.Value.Trim() == "")
                {
                    return null;
                }
                return Convert.ToInt32(FORM_KEY6.Value);
            }
        }

        public string DetailsCode
        {
            get
            {
                return FORM_KEY7.Value;
            }
        }

        public int? PLANAPP_ID
        {
            get
            {
                if (FORM_KEY8.Value.Trim() == "")
                {
                    return null;
                }
                return Convert.ToInt32(FORM_KEY8.Value);
            }
        }

        private YX_BANK_LISTBLL bll = new YX_BANK_LISTBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["TYPE"] == "API")
                {
                    var requstData = AjaxResult.GetPostData(this.Context);
                    var data = new JObject();
                    data["IfFirst"] = requstData["IfFirst"].NewToObject<bool>();

                    var typeCode = requstData["TYPE_CODE"].NewToObject<string>();
                    var typeKey = requstData["TYPE_KEY"].NewToObject<int>();
                    var companyId = requstData["CMY_ID"].NewToObject<int>();
                    var year = requstData["YEAR"].NewToObject<int>();
                    var month = requstData["MONTH"].NewToObject<int?>();
                    var week = requstData["WEEK"].NewToObject<int?>();
                    var proId = requstData["PRO_ID"].NewToObject<int?>();
                    string parm1 = null;
                    string parm2 = null;
                    var orderBy = requstData["SortStr"].NewToObject<string>();
                    var skip = requstData["PageSize"].NewToObject<int>() * (requstData["CurrentPage"].NewToObject<int>() - 1);
                    var take = requstData["PageSize"].NewToObject<int>();
                    switch (typeKey)
                    {
                        case 1:
                            parm1 = requstData["TYPE_NAME"].NewToObject<string>();
                            break;

                        case 2:
                            parm1 = requstData["USER_TYPE"].NewToObject<string>();
                            break;

                        case 3:
                        case 4:
                            parm1 = requstData["CUSTOMER_NAME"].NewToObject<string>();
                            break;

                        case 5:
                        case 6:
                            parm1 = requstData["CUSTOMER_NAME"].NewToObject<string>();
                            parm2 = requstData["PROJECT_NAME"].NewToObject<string>();
                            break;

                        default:
                            break;
                    }
                    if (requstData["IfFirst"].NewToObject<bool>())
                    {
                        data["Count"] = bll.GetDataByCountDetail(
                                    typeKey,
                                    year,
                                    month,
                                    week,
                                    proId,
                                    companyId,
                                    parm1,
                                    parm2
                                    );
                    }
                    data["Data"] = JToken.FromObject(bll.GetDataByPage(typeKey,
                                    year,
                                    month,
                                    week,
                                    proId,
                                    companyId,
                                    parm1,
                                    parm2,
                                    orderBy,
                                    skip, take));
                    AjaxResult.Success(this.Context, data);
                    return;
                }

                ViewState["SortOrder"] = "n_detail_listid";
                ViewState["OrderDire"] = " desc ";
                FORM_KEY4.Value = Request.QueryString["PRO_ID"];
                if (!string.IsNullOrEmpty(Request.QueryString["COMPANY_ID"]))
                {
                    FORM_KEY5.Value = Request.QueryString["COMPANY_ID"];
                }
                else
                {
                    FORM_KEY5.Value = lognUser.USER_COMPANY.ToString();
                }
                switch (Request.QueryString["ISTRUE"])
                {
                    case "SUM_YEAR":
                        {
                            FORM_KEY1.Value = Request.QueryString["YEAR"];
                        }
                        break;

                    case "SUM_MONTH":
                        {
                            FORM_KEY1.Value = Request.QueryString["YEAR"];
                            FORM_KEY2.Value = Request.QueryString["MONTH"];
                        }
                        break;

                    case "SUM_WEEK":
                        {
                            FORM_KEY1.Value = Request.QueryString["YEAR"];
                            FORM_KEY2.Value = Request.QueryString["MONTH"];
                            FORM_KEY3.Value = Request.QueryString["WEEK"];
                        }
                        break;

                    case "YEAR":
                        {
                            FORM_KEY1.Value = Request.QueryString["YEAR"];
                            FORM_KEY6.Value = Request.QueryString["NId"];
                            FORM_KEY7.Value = Request.QueryString["DetailsCode"];
                            FORM_KEY8.Value = Request.QueryString["PLANAPP_ID"];
                        }
                        break;

                    case "MONTH":
                        {
                            FORM_KEY1.Value = Request.QueryString["YEAR"];
                            FORM_KEY2.Value = Request.QueryString["MONTH"];
                            FORM_KEY6.Value = Request.QueryString["NId"];
                            FORM_KEY7.Value = Request.QueryString["DetailsCode"];
                            FORM_KEY8.Value = Request.QueryString["PLANAPP_ID"];
                        }
                        break;

                    case "WEEK":
                        {
                            FORM_KEY1.Value = Request.QueryString["YEAR"];
                            FORM_KEY2.Value = Request.QueryString["MONTH"];
                            FORM_KEY3.Value = Request.QueryString["WEEK"];
                            FORM_KEY6.Value = Request.QueryString["NId"];
                            FORM_KEY7.Value = Request.QueryString["DetailsCode"];
                            FORM_KEY8.Value = Request.QueryString["PLANAPP_ID"];
                        }
                        break;

                    default:
                        break;
                }
                if (FORM_KEY6.Value == "" || FORM_KEY7.Value == "" || FORM_KEY8.Value == "")
                {
                    FORM_SUM.Text = Utility.Helper.HtmlHelper.QyFormat(bll.GetDataBySum(GetYear, GetMonth, GetWeek, GetProId, GetCompanyId));
                }
                else
                {
                    FORM_SUM.Text = Utility.Helper.HtmlHelper.QyFormat(bll.GetDataByDetail(GetYear, GetMonth, GetWeek, GetProId, GetCompanyId, NId, DetailsCode, PLANAPP_ID));
                }
                //BindData(true);
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            //BindData(false);
        }

        /*
        private void BindData(bool isFirst)
        {
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }

            var count = 0;
            var dt = new DataTable();
            if (NId == null || DetailsCode == null || PLANAPP_ID == null)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                count = bll.GetDataByCount(GetYear, GetMonth, GetWeek, GetProId, GetCompanyId);
                dt = bll.GetDataByPage(GetYear, GetMonth, GetWeek, GetProId, GetCompanyId, sort, (PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage);
                var sum = dt.AsEnumerable().Sum(a => a["N_AMT"] == DBNull.Value ? 0 : Convert.ToDecimal(a["N_AMT"])) / 10000;
                FORM_SUM1.Text = Utility.Helper.HtmlHelper.QyFormat(sum);
            }
            else
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                count = bll.GetDataByCountDetail(GetYear, GetMonth, GetWeek, GetProId, GetCompanyId, NId, DetailsCode, PLANAPP_ID);
                dt = bll.GetDataByPageDetail(GetYear, GetMonth, GetWeek, GetProId, GetCompanyId, sort, (PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage, NId, DetailsCode, PLANAPP_ID);
                var sum = dt.AsEnumerable().Sum(a => a["N_AMT"] == DBNull.Value ? 0 : Convert.ToDecimal(a["N_AMT"])) / 10000;
                FORM_SUM1.Text = Utility.Helper.HtmlHelper.QyFormat(sum);
            }
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = count;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                grvShow1.DataSource = null;
                grvShow1.DataBind();
                PageIng.Visible = false;
            }
        }
        */

        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //点击排序
            string sPage = e.SortExpression;

            if (sPage == "")
            {
                return;
            }
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
                ViewState["OrderDire"] = "ASC";
            }

            //BindData(true);
        }
    }
}